<template>
    <el-dialog title="新增权限组" :visible.sync="dialogVisible" width="700px">
        <div style="max-height: 400px; overflow-y: auto;">
            <div style="overflow: auto; max-height: 600px;">
                <!-- 表单内容 -->
                <el-form ref="form" :model="form" label-width="80px" size="small">
                    <el-form-item label="角色名称" prop="name"
                                  :rules="[{ required: true, message: '角色名称不能为空', trigger: 'blur' },]">
                        <el-input v-model="form.name"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" prop="remark">
                        <el-input v-model="form.remark"></el-input>
                    </el-form-item>
                    <el-form-item label="权限" prop="remark">
                        <el-tree
                            default-expand-all
                            ref="tree"
                            :data="permitData"
                            show-checkbox
                            node-key="id"
                            :props="{
                                    children: 'children',
                                    label: 'name'}"
                            @check="handleCheckedChange"
                        ></el-tree>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <div></div>
                <div>
                    <el-button @click="cancel" size="small">取 消</el-button>
                    <el-button type="primary" @click="clickToAddRole" size="small">确 定</el-button>
                </div>
            </div>
        </div>
    </el-dialog>
</template>

<script>
import {upDateStaffRole} from "@/index/api/index/setting/staffRole/index";
import {menuList} from "@/index/api/menu/menu"

export default {
    data() {
        return {
            dialogVisible: false,
            form: {
                name: "",
                remark: "",
                permit: ""
            },
            permitData: [],
            checkedNodes: []
        }
    },
    mounted() {
        this.getData()
    },
    methods: {
        showDialog(e, row) {
            this.form = row
            setTimeout(_ => {
                this.$refs.tree.setCheckedKeys(row.permit.split(","))
            }, 500)
            this.dialogVisible = true;
        },
        handleCheckedChange(data, checked) {
            if (checked) {
                // 如果节点被选中，将其添加到选中节点数组中
                this.checkedNodes.push(data);
            } else {
                // 如果节点被取消选中，从选中节点数组中移除
                const index = this.checkedNodes.findIndex(node => node.id === data.id);
                if (index !== -1) {
                    this.checkedNodes.splice(index, 1);
                }
            }
        },
        cancel() {
            this.dialogVisible = false;
            this.$refs.form.resetFields();
        },
        clickToAddRole() {
            if (this.$refs.form) {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        this.form.permit = this.$refs.tree.getCheckedKeys().join(",")
                        upDateStaffRole(this.form, this.form.id).then((res) => {

                            if (res.code == 200) {
                                this.dialogVisible = false;
                                this.$refs["form"].resetFields();
                                this.$message({
                                    message: res.message,
                                    type: "success",
                                });
                                this.$refs.tree.setCheckedKeys([]);
                                this.$parent.getOperatorList();
                            } else {
                                this.$message({
                                    message: res.message,
                                    type: "error",
                                });
                            }
                        });
                    } else {
                        // 表单验证失败后的逻辑
                        this.$message({
                            message: '表单验证失败，请检查输入',
                            type: 'error',
                        });
                    }
                });
            }
        },
        getData() {
            menuList({mode: "tree"}).then((res) => {
                this.permitData = res.data
            });
        }
    }

}
</script>

<style lang="less" scoped>
.dialog-footer {
    display: flex;
    justify-content: space-between;
    margin-right: 27px;
}
</style>
